package client;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import customLayouts.VerticalFlowLayout;

import model.Sensor;
import model.SensorController;
import model.SensorController.SensorControllerListener;

public class Tab1 extends JComponent implements SensorControllerListener {

	private static final long serialVersionUID = 1L;
	private JPanel reactorbeneden, reactorcore, reactorsurface, thermalstatus;
	private ArrayList<Sensor> sensors;
	private int drawTeller = 0;	
	
	public Tab1(){
		//get the sensorcontroller so we can access the sensor values
		SensorController sensorcontroller = SensorController.getInstance();
		// Add a listener for the SensorController
		sensorcontroller.addSensorControllerListener(this);
		
		sensors = sensorcontroller.getSensorArray();
		
        setLayout(new GridLayout(0,1));
        //Border lineBorder = BorderFactory.createLineBorder(Color.BLACK);
    	
        //panel with the two top sensor blocks and the other stuff
        JPanel top = new JPanel(false);
        JPanel topleft = new JPanel(false);
        topleft.setLayout(new GridLayout(0,1));
        
        //bovenste blok met sensorwaarden
        reactorbeneden = new JPanel(false);
        reactorbeneden.setBorder(titleborder("Reactor Beneden"));
        //fillPanelTop();
        
        
        //tweede blok met sensorwaarden
        reactorcore = new JPanel(false);
        reactorcore.setBorder(titleborder("Reactor Core"));
        //fillPanelTwo();
        
        topleft.add(reactorbeneden);
        topleft.add(reactorcore);
        top.add(topleft);
        top.add(userPanel());
        
        
        //derde blok met sensorwaarden
        reactorsurface = new JPanel(false);
        reactorsurface.setBorder(titleborder("Reactor Core"));
        //fillPanelThree();
        
        //vierde, laatste blok met sensorwaarden
        thermalstatus = new JPanel(false);
        thermalstatus.setBorder(titleborder("Reactor Core"));
        //fillPanelFour();
                
        add(top);
        add(reactorsurface);
        add(thermalstatus);
		        
	}
	
	public void fillPanelTop(){
		reactorbeneden.removeAll();
		for(drawTeller = 0;drawTeller<5;drawTeller++){
        	reactorbeneden.add(sensors.get(drawTeller).drawOnScreen());
        }
	}
	
	public void fillPanelTwo(){
		reactorcore.removeAll();
		for(drawTeller = 5;drawTeller<10;drawTeller++){
			reactorcore.add(sensors.get(drawTeller).drawOnScreen());
        }
	}
	
	public void fillPanelThree(){
		reactorsurface.removeAll();
		for(drawTeller = 10;drawTeller<17;drawTeller++){
        	reactorsurface.add(sensors.get(drawTeller).drawOnScreen());
        }
	}
	
	public void fillPanelFour(){
		thermalstatus.removeAll();
		for(drawTeller = 17;drawTeller<24;drawTeller++){
			thermalstatus.add(sensors.get(drawTeller).drawOnScreen());
        }
	}
		
	private Border titleborder(String title){
    	return BorderFactory.createTitledBorder(title);
    }
	
	private JComponent userPanel(){
    	JPanel panel = new JPanel(false);
    	panel.setLayout(new VerticalFlowLayout());
    	
    	JPanel timepanel = new JPanel(false);
    	timepanel.setLayout(new VerticalFlowLayout());
        timepanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        JLabel datetime = new JLabel(Long.toString(new Date().getTime()));
        JLabel watt = new JLabel("2004 kW");
        timepanel.add(datetime);
        timepanel.add(watt);
        
        JPanel buttonpanel = new JPanel(false);
        JButton LogButton = new JButton("Log");
        JButton ExportButton = new JButton("Export");
        JButton WarningButton = new JButton("Warnings");
        JButton RefreshButton = new JButton("Refresh");
        buttonpanel.add(LogButton);
        buttonpanel.add(ExportButton);
        buttonpanel.add(WarningButton);
        buttonpanel.add(RefreshButton);
        
        WarningButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new WarningWindow();
				
			}
		});
        
        RefreshButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				fillPanelTop();
				fillPanelTwo();
				fillPanelThree();
				fillPanelFour();
				IRIclient.frame.setVisible(true);
			}
		});
        
        panel.add(timepanel);
        panel.add(buttonpanel);
        
        return panel;        
    }
	

	/**
	 * Called when sensor data in the SensorController changes.
	 */
	@Override
	public void sensorDataChanged(SensorController source) {
		sensors = source.getSensorArray();
		drawTeller = 0;
		fillPanelTop();
		fillPanelTwo();
		fillPanelThree();
		fillPanelFour();
		repaint();
	}
}
